package cn.ligoo.part.web.springmvc.platform;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.MessageSource;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import cn.ligoo.part.domain.UserInfo;
import cn.ligoo.part.service.impl.FacadeImpl;
import cn.ligoo.part.web.Constants;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

@Controller
@RequestMapping("/platform/profile")
public class ProfileController {

	private final Logger logger = LoggerFactory.getLogger(ProfileController.class);

	@Resource
	private MessageSource message;

	@Resource
	private FacadeImpl facade;

	public ProfileController() {
		logger.debug("...constructor");
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String save(HttpServletRequest request, UserInfo userInfo) {
		logger.debug("...save");
		logger.debug("userInfo:{}", userInfo.toString());
		try {
			if (null == userInfo.getId()) {
				userInfo.setId(facade.getUserInfoService().createUserInfo(userInfo));
			} else {
				facade.getUserInfoService().modifyUserInfo(userInfo);
			}
			// 更新 Session
			UserInfo t = new UserInfo();
			t.setId(userInfo.getId());
			WebUtils.setSessionAttribute(request, Constants.SESSION_USER_INFO,
					facade.getUserInfoService().getUserInfo(t));
		} catch (DataAccessException dae) {
			String msg = message.getMessage("errors.dae", null, null);
			return String.format(Constants.JSON_ERROR, msg);
		}
		String msg = message.getMessage("profile.update.success", null, null);
		return String.format(Constants.JSON_SUCCESS, msg, null);
	}

	@RequestMapping(value = "/edit", produces = "text/html;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public String edit(HttpServletRequest request) {
		logger.debug("...edit");
		UserInfo userInfo = (UserInfo) WebUtils.getSessionAttribute(request, Constants.SESSION_USER_INFO);
		logger.debug("...userInfo:{}", userInfo);

		SerializeConfig sc = new SerializeConfig();
		sc.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd"));
		return JSON.toJSONString(userInfo, sc);
	}
}
